<?php
namespace Api\Controller;

use Api\Model\OrderModel;

class CommentController extends ApiController
{

    /**
     * 提交评论
     * 
     * @param
     *            int order_id
     * @param
     *            int product_id
     * @param
     *            string content
     * @param
     *            int cover_id
     * @param
     *            int rating_desc 描述得分
     * @param
     *            int rating_attitude 服务态度得分
     * @param
     *            int rating_speed 发货速度得分
     * @param
     *            int rating_shipping 物流得分
     * @param
     *            int rate 满意度 1非常满意 2 一般 3 差
     */
    public function postcomment()
    {
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        $data = params_post(array(
            'order_id',
            'product_id',
            'content',
            'rating_desc',
            'rating_attitude',
            'rating_speed',
            'rating_shipping',
            'rate'
        ));
        
        $cover_id = I('post.cover_id');
        is_array($cover_id) && $cover_id = implode(',', $cover_id);
        $data['cover_ids'] = $cover_id;
        $data['create_time'] = NOW_TIME;
        $data['uid'] = API_UID;
        
        $M_product_comment = M('product_comment');
        $M_order = M("order");
        $M_order_product = M('order_product');
        
        $order_info = $M_order->find($data['order_id']);
        empty($order_info) && response(- 2, '', '不存在的订单 ');
        $order_info['order_status'] == OrderModel::ORDER_COMPLATED || response(- 1, '', '此订单暂时不能评论');
        
        // 验证是否是订单中的商品
        $exit_order_product = $M_order_product->where(array('order_id' => $data['order_id'],'product_id' => $data['product_id']))->find();
        empty($exit_order_product) && response(- 1, '', '评论的商品不在订单中');
        
        $product_comment_info = $M_product_comment->where(array('order_id'=>$data['order_id'],'product_id'=>$data['product_id']))->find();
        $product_comment_info && response(-2, null, '订单商品已评价，请不要重复评价');
        
        $product_val_map = array();
        $product_val_map['product_id'] = $data['product_id'];
        $product_val_map['order_id'] = $data['order_id'];
        
        $is_order_product = $M_order_product->where($product_val_map)->find();
        ! $is_order_product && response(- 2, '', '订单中不存在此商品');
        ! preg_match('/^[1-3]?$/', $data['rate']) && response(- 1, '', '满意度评分值错误');
        ! preg_match('/^[1-5]?$/', $data['rating_desc']) && response(- 1, '', '描述得分值错误');
        ! preg_match('/^[1-5]?$/', $data['rating_attitude']) && response(- 1, '', '服务得分值错误');
        ! preg_match('/^[1-5]?$/', $data['rating_speed']) && response(- 1, '', '发货速度得分值错误');
        ! preg_match('/^[1-5]?$/', $data['rating_shipping']) && response(- 1, '', '物流得分值错误');
        
        $res = $id = $M_product_comment->add($data);
        $res !== false || response(-9);
        $data['is_saler'] = 0;
        $data['comment_id'] = $id;
        M('product_comment_message')->add($data);
        M('order')->where(array('id'=>$data['order_id']))->setField('review_status', OrderModel::REVIEW_REVIEWED);
        $M_order_product->where(array('order_id'=>$data['order_id'],'product_id' => $data['product_id']))->setfield('is_comment', 1);
        
        response(1, '', '评论成功');
    }
    
    public function moreComment() {
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        $data = I('post.');
        $order_info = M('order')->where(array('id'=>$data['order_id']))->find();
        $order_info || response(-2, null, '订单不存在');
        $order_info['uid'] == API_UID || response(-2, null, '无权操作此订单');
        $order_info['review_status'] == OrderModel::REVIEW_UNREVIEWED && response(-2, null, '订单尚未评价，不能追加评论');
        $product_comment_info = M('product_comment')->where(array('order_id'=>$data['order_id'],'product_id'=>$data['product_id']))->find();
        $product_comment_info || response(-2, null, '订单尚未评价，不能追加评论');
        
        $model = M();
        $model->check($data['content'], '1,200', 'length') || response(-1, null, '评论内容长度在1-200个字符之间');
        $data['comment_id'] = $product_comment_info['id'];
        $data['uid'] = API_UID;
        $data['create_time'] = NOW_TIME;
        $data['is_saler'] = 0;
        is_array($data['cover_ids']) && $data['cover_ids'] = implode(',', $data['cover_ids']);
        $res = $id = M('product_comment_message')->add($data);
        $res !== false ? response(1) : response(-9);
    } 
    
    /**
     * 获取商品评论
     * 
     * @param
     *            int product_id
     * @param
     *            int rate
     * @param
     *            int page
     * @param
     *            int page_size
     */
    public function getProductComment()
    {
        $product_id = I('post.product_id');
        
        params_exist(array(
            'product_id'
        ));
        $page = I('post.page', 1);
        $page_size = I('post.page_size', 10);
        $offset = ($page - 1) * $page_size;
        
        $rate = I('post.rate');
        
        $M_product_comment = M('product_comment');
        $map = array();
        $map['product_id'] = $product_id;
        if ($rate) {
            $map['rate'] = $rate;
        }
        
        $list = $M_product_comment->limit($offset, $page_size)->where($map)->select();
        $total = $M_product_comment->where($map)->count() | 0;
        
        $result = array();
        $result['total'] = $total;
        foreach ($list as &$item) {
            $item['avatar'] = get_avatar($item['uid']);
            $nickname = get_nickname($item['uid']);
            $item['username'] = substr_replace($nickname, '****', - 4);
            $item['format_time'] = time_format($item['create_time'], "Y-m-d h:s:m");
            $images = array();
            $images_id = explode(',', $item['cover_ids']);
            foreach ($images_id as $cover_id) {
                $images[] = get_url($cover_id);
            }
            $item['images'] = $images;
            $item['messages'] = M('product_comment_message')->where(array('commnet_id'=>$item['id']))->select();
        }
        $result['result'] = $list;
        $list ? response(1, $result) : response(- 2);
    }
}

?>